// 写cookies
function setCookie (name, value, day) {
    if (day !== 0) { // 当设置的时间等于0时，不设置expires属性，cookie在浏览器关闭后删除
        var expires = day * 24 * 60 * 60 * 1000
        var date = new Date(+new Date() + expires)
        document.cookie = name + '=' + escape(value) + ';expires=' + date.toUTCString() + ';path=/'
    } else {
        document.cookie = name + '=' + escape(value)
    }
}

// 读cookies
function getCookie (name) {
    var arr = null
    var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
    arr = document.cookie.match(reg)
    if (arr) { return unescape(arr[2]) } else { return null }
}

// 删除cookies
function delCookie (name) {
    var expires = -1 * 24 * 60 * 60 * 1000
    var date = new Date(+new Date() + expires)
    document.cookie = name + '=' + escape('') + ';expires=' + date.toUTCString() + ';path=/'
}

function timerFilter (value) {
    let t = new Date(value)
    let years = t.getFullYear()
    let months = t.getMonth() + 1 > 9 ? t.getMonth() + 1 : '0' + (t.getMonth() + 1)
    let dates = t.getDate() > 9 ? t.getDate() : '0' + t.getDate()
    let hours = t.getHours() > 9 ? t.getHours() : '0' + t.getHours()
    let minutes = t.getMinutes() > 9 ? t.getMinutes() : '0' + t.getMinutes()
    let seconds = t.getSeconds() > 9 ? t.getSeconds() : '0' + t.getSeconds()
    return years + '-' + months + '-' + dates + ' ' + hours + ':' + minutes + ':' + seconds
}

export {
    setCookie,
    getCookie,
    delCookie,
    timerFilter
}
